package com.p7.notepad.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;
import com.p7.notepad.R;
import com.p7.notepad.app.BaseActivity;
import com.p7.notepad.bean.Login;
import com.p7.notepad.bean.User;
import com.p7.notepad.network.APIManager;
import com.p7.notepad.network.exception.ExceptionHandle;
import com.p7.notepad.network.listener.RequestListener;
import com.p7.notepad.tools.Storage;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity {

    @BindView(R.id.login_user)
    EditText loginUser;
    @BindView(R.id.login_pwd)
    EditText loginPwd;
    @BindView(R.id.login_btn)
    Button loginBtn;
    @BindView(R.id.login_register)
    Button loginRegister;

    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initEvents() {
        super.initEvents();
        Bundle bundle = getIntent().getBundleExtra("bundle");
        if (bundle != null) {
            loginUser.setText(bundle.getString("mobile"));
        }
    }

    @OnClick({R.id.login_btn, R.id.login_register})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.login_btn:
                submit();
                break;
            case R.id.login_register:
                startActivity(RegisterActivity.class);
                break;
        }
    }

    private void submit() {
        String user = loginUser.getText().toString().trim();
        if (TextUtils.isEmpty(user)) {
            Toast.makeText(this, "手机号不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        String pwd = loginPwd.getText().toString().trim();
        if (TextUtils.isEmpty(pwd)) {
            Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        Map<String, String> parameters = new HashMap<>();
        parameters.put("mobile", user);
        parameters.put("password", pwd);
        APIManager.onPost("user/login", parameters, new RequestListener() {
            @Override
            public void onSuccess(String s) {
                onGson(s);
            }

            @Override
            public void onError(ExceptionHandle.ResponeThrowable error) {
                setToast(error.message);
            }
        });

    }

    private void onGson(String s) {
        Gson gson = new Gson();
        Login login = gson.fromJson(s, Login.class);
        if (login.getCode() == 1000) {
            setToast("登录成功");
            User user = new User();
            user.setId(login.getData().getId());
            user.setAvatar(login.getData().getAvatar());
            user.setMobile(login.getData().getMobile());
            user.setNickName(login.getData().getNickName());
            Storage.putUserList(user);

            Storage.putUser(login.getData());
            Storage.putName(login.getData().getNickName());
            Storage.putImage(login.getData().getAvatar());
            Storage.putId(login.getData().getId());
            startActivity(MainActivity.class);
            finish();
        } else {
            setToast(login.getMessage() + "请稍后重试");
        }
    }

    private void setToast(String str) {
        Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
    }

}
